//
//  BBHomeTableViewCell.m
//  BlockBrowser
//
//  Created by Mac on 2018/3/30.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "BBHomeTableViewCell.h"
#import <WebKit/WebKit.h>
#define DeFultlineSpacing 5
@interface  BBHomeTableViewCell()
@property(nonatomic, strong) UIView                     *line;
@property(nonatomic, strong) STButton                     *timeButton;

@property(nonatomic, strong) WKWebView                     *webView;
@property(nonatomic, strong) STLabel                     *yueduLable;/**< 阅读全文lable */


@end
@implementation BBHomeTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self configSubView];
    }
    return self;
}
+ (CGFloat)cellHeight{
    NSInteger row = [BBSystemManger manger].news_line.integerValue;
    if (!row) {
        row = 5;
    }
    return 230 + (row - 1) * DeFultlineSpacing;
}
//处理 1https://blog.csdn.net/jq2530469200/article/details/51375484 2http://www.cocoachina.com/bbs/read.php?tid-1687896-page-1.html  3
- (void)setModel:(BBArticleModel *)model{
    _model = model;
    
    
    [model timerEnd];
    NSInteger font = [BBSystemManger manger].news_font_size.integerValue;
    NSInteger row = [BBSystemManger manger].news_line.integerValue;
    self.contenLable.font = [UIFont systemFontOfSize:font];
    
    NSMutableAttributedString * attrStr = [self attributedStringFormH5:model.content];
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.lineSpacing = DeFultlineSpacing;
    paraStyle.hyphenationFactor = 1;
    NSMutableAttributedString * finsh =   [[NSMutableAttributedString alloc] initWithAttributedString:attrStr];
    [finsh addAttributes:@{NSParagraphStyleAttributeName:paraStyle,NSKernAttributeName:@0.1f
                           } range:NSMakeRange(0, finsh.length)];
    [finsh addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} range:NSMakeRange(0, finsh.length)];
    self.contenLable.attributedText = finsh;
    
    CGFloat allheight =   [self.contenLable.attributedText.string st_heigthWithwidth:self.contenLable.width font:font];
    allheight = [self getSpaceLabelHeight:self.contenLable.attributedText.string withFont:[UIFont systemFontOfSize:font] withWidth:self.contenLable.width];
    model.currentLableHeight = allheight;
    
    if (model.isSelcted) {
        self.contenLable.height = allheight ;
        self.contenLable.numberOfLines = 0;
    }else{
        self.contenLable.height = 20 * row + 20 + (row )  * DeFultlineSpacing ;
        self.contenLable.numberOfLines = row + 1;
        CGFloat version =  [[[UIDevice currentDevice] systemVersion] doubleValue] ;
        if (UIScreenWidth > 375 && version>= 10 && version <=11) {
            //plus 多一行
            self.contenLable.numberOfLines = row;
        }
        model.originLableHeight = self.contenLable.height;
        
    }
    if (model.originLableHeight < model.currentLableHeight) {
        
        if (model.isSelcted) {
            self.yueduLable.hidden = YES;
        }else{
            self.yueduLable.hidden = NO;
        }
        self.yueduLable.top = self.contenLable.bottom - 25;
    }else{
        self.yueduLable.hidden = YES;
    }
    
    if (model.canCountDown) {
        [self.shareButton setTitle:@"分享挖矿"
                          forState:UIControlStateNormal];
        [self.shareButton setTitleColor:TM_redColor forState:UIControlStateNormal];
    }else{
        [self.shareButton setTitle:@"分享挖矿"
                          forState:UIControlStateNormal];
        [self.shareButton setTitleColor:TM_secendTextColor forState:UIControlStateNormal];
    }
    
    NSString * time =  model.dateline;
    NSString * lastTime = [time componentsSeparatedByString:@" "].lastObject;
    NSArray * lastArray = [lastTime componentsSeparatedByString:@":"];
    lastTime = [NSString stringWithFormat:@"%@:%@",lastArray.firstObject,lastArray[1]];
    [self.timeButton setTitle:lastTime forState:UIControlStateNormal];
    [self.detailTimeButton setTitle:[time componentsSeparatedByString:@" "].firstObject forState:UIControlStateNormal];
    
    [self.lastButton setTitle:[NSString stringWithFormat:@"剩余%@  ",model.coin_num] forState:UIControlStateNormal];
    [self.detailTimeButton sizeToFit];
    [self.lastButton sizeToFit];
    [self.shareButton sizeToFit];
    self.lastButton.hidden = !model.coin_num.integerValue;
    
    self.shareButton.width = self.shareButton.width +15;
    self.shareButton.right = UIScreenWidth ;
    
    self.lastButton.right = self.shareButton.left - 15;
    self.detailTimeButton.width = self.detailTimeButton.width + 10;
    self.detailTimeButton.right = self.lastButton.left - 15;
    
//    self.detailTimeButton.top = self.contenLable.bottom +5; 阅读全文 位置下落
    self.yueduLable.top = self.contenLable.bottom;
    self.detailTimeButton.top = self.contenLable.bottom +25;
    
    
    self.shareButton.centerY = self.detailTimeButton.centerY;
    self.lastButton.centerY = self.detailTimeButton.centerY;
    
    [self.lastButton makeImageRight];
    if (model.isSelcted) {
        model.allHeight = self.shareButton.bottom + 25;
    }
    self.line.height  = self.shareButton.bottom + 50;
    
    if (model.canCountDown) {
        __weak typeof(self) weakSelf =  self;
        [self.shareButton setTitle:@"分享挖矿"
                          forState:UIControlStateNormal];
        [self.shareButton setTitleColor:TM_redColor forState:UIControlStateNormal];
        [self.detailTimeButton setTitle:[model currentDetailTime]forState:UIControlStateNormal];
        [model timerBegain];
        [model setCountDown:^(NSString *dateString) {
            if (weakSelf.model.canCountDown) {
                [weakSelf.detailTimeButton setTitle:dateString forState:UIControlStateNormal];
            }
            
        }];
    }else{
        [model timerEnd];
        [self.shareButton setTitle:@"分享挖矿"
                          forState:UIControlStateNormal];
        [self.shareButton setTitleColor:TM_secendTextColor forState:UIControlStateNormal];
        [self.detailTimeButton setTitle:[time componentsSeparatedByString:@" "].firstObject forState:UIControlStateNormal];
    }
    
}
#pragma mark --subView
- (void)configSubView{
    
    self.backgroundColor = [BBThemeColorManger manger].backGroundColor;
    self.line = [[UIView alloc] initWithFrame:CGRectMake(15, 0, 0.5, [BBHomeTableViewCell cellHeight])];
    self.line.backgroundColor = TM_lineColor;
    [self addSubview:self.line];
    
    UIView * roundView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, 6, 6)];
    roundView.centerX = self.line.centerX;
    roundView.clipsToBounds = YES;
    roundView.layer.cornerRadius = 3;
    roundView.backgroundColor = UIColorFromRGBA(0xFAA141);
    [self addSubview:roundView];
    
    self.timeButton = [[STButton alloc] initWithFrame:CGRectMake(self.line.right + 5, 0, 56, 17)
                                                title:@"11:12"
                                           titleColor:FirstTextColor
                                            titleFont:12
                                         cornerRadius:0
                                      backgroundColor:nil
                                      backgroundImage:[UIImage imageNamed:@"多边形1"]
                                                image:nil];
    
    self.timeButton.centerY = roundView.centerY;
    self.timeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self addSubview:self.timeButton];
    
    NSString * content =@"北京时间3月14日中午，多家媒体确认，著名物理学家史蒂芬.霍金去世，享年76岁是剑桥大学数1942年1月8日出生于英国牛津的霍金，及理论物理学系教授，当代最重要的广义相对论和宇宙论科学家。荣获英国剑桥大学卢卡斯数学教席，这是自然科学史上继牛顿和狄拉克之后荣誉最高的教席。";
    NSInteger row = [BBSystemManger manger].news_line.integerValue;
    self.contenLable = [[STLabel alloc] initWithFrame:CGRectMake(self.line.right + 10, self.timeButton.bottom+10, UIScreenWidth - 20 - self.line.right, row * 20 + (row+1) * 2 * DeFultlineSpacing)
                                                 text:content
                                            textColor:[BBThemeColorManger manger].firstTextColor
                                                 font:16
                                          isSizetoFit:NO
                                        textAlignment:NSTextAlignmentLeft];
    [self addSubview:self.contenLable];
    self.contenLable.lineBreakMode = NSLineBreakByCharWrapping;
    self.contenLable.verticalAlignment = VerticalAlignmentTop;
    
    self.webView = [[WKWebView alloc] initWithFrame:self.contenLable.frame];
    self.webView.backgroundColor = TM_lineColor;
    self.webView.userInteractionEnabled = NO;
    self.webView.backgroundColor = [UIColor clearColor];
    // [self addSubview:self.webView];
    
    self.shareButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 80, 30)
                                                 title:@"分享"
                                            titleColor:SecendTextColor
                                             titleFont:14
                                          cornerRadius:0
                                       backgroundColor:nil
                                       backgroundImage:nil
                                                 image:[UIImage imageNamed:@"分享(1)拷贝"]];
    // [self.shareButton sizeToFit];
    self.shareButton.width = self.shareButton.width + 10;
    self.shareButton.right = UIScreenWidth - 15;
    self.shareButton.top = self.contenLable.bottom + 10;
    self.shareButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    [self addSubview:self.shareButton];
    
    
    self.lastButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 120, self.shareButton.height)
                                                title:@"剩余10"
                                           titleColor:SecendTextColor
                                            titleFont:14
                                         cornerRadius:0
                                      backgroundColor:nil
                                      backgroundImage:nil
                                                image:[UIImage imageNamed:@"64-金币"]];
    self.lastButton.right = self.shareButton.left - 15;
    self.lastButton.top = self.contenLable.bottom + 10;
    self.lastButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    [self addSubview:self.lastButton];
    
    
    self.detailTimeButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 120, self.shareButton.height)
                                                      title:@"15:28:38"
                                                 titleColor:SecendTextColor
                                                  titleFont:14
                                               cornerRadius:0
                                            backgroundColor:nil
                                            backgroundImage:nil
                                                      image:[UIImage imageNamed:@"沙漏"]];
    self.detailTimeButton.right = self.lastButton.left - 15;
    self.detailTimeButton.top = self.contenLable.bottom + 10;
    self.detailTimeButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    [self addSubview:self.detailTimeButton];
    
    
    self.yueduLable = [[STLabel alloc] initWithFrame:CGRectMake(self.contenLable.left, self.contenLable.bottom, 200, 20)
                                                     text:@"....[阅读全文]"
                                                textColor:UIColor.blueColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [self addSubview:self.yueduLable];
    
}
- (NSMutableAttributedString*)attributedStringFormH5:(NSString*)h5String{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.lineSpacing = 120;
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[h5String dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType ,NSFontAttributeName:[UIFont systemFontOfSize:14],NSParagraphStyleAttributeName:paraStyle} documentAttributes:nil error:nil];
    [attrStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(0, attrStr.length)];
    [attrStr addAttributes:@{NSParagraphStyleAttributeName:paraStyle,NSKernAttributeName:@1.5f} range:NSMakeRange(0, attrStr.length)];
    return attrStr;
    
}
-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width {
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    
    paraStyle.alignment = NSTextAlignmentLeft;
    
    paraStyle.lineSpacing = DeFultlineSpacing;
    
    
    paraStyle.firstLineHeadIndent = 0.0;
    
    paraStyle.paragraphSpacingBefore = 0.0;
    
    paraStyle.headIndent = 0;
    
    paraStyle.tailIndent = 0;
    
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
                          };
    
    
    
    CGSize size = [str boundingRectWithSize:CGSizeMake(width, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
    return size.height;
    
}

@end

